#include "uzawagaussseidelsolver.h"
#include <cmath>
#include <iostream>
#include <QThread>

using namespace std;

UzawaGaussSeidelSolver::UzawaGaussSeidelSolver(BaseGaussSeidelSolver * in_slvr, QObject * parent):
        SimpleSolver(in_slvr, parent), ro(0), uzawaEps(1.0e-2), isStopped(false)//, inner_solver(in_slvr)
{

}

void UzawaGaussSeidelSolver::run()
{
//    int iter = 0;
//    /QVector<double> lOld;
//    lOld.resize(m);
//    for (int i = 0; i < m; i++)
//        lOld[i] = 0;
//    for (int i = 0; i < m; i++)
//        l[i] = 0;
    double outer_dx = 1000000;
    while (outer_dx > uzawaEps)
    {
        if (isStopped)
            return ;
        inner_solver->solve();
        outer_dx = inner_solver->updateLagrange(ro);
        cout << "OUTER DX = " << outer_dx << endl;
        //RotatedGaussSeidelSolver2::run();
//        outer_dx = 0;
//        for (int i = 0; i < m; i++)
//        {
//            if (abs(l[i] - lOld[i]) > outer_dx)
//                outer_dx = abs(l[i] - lOld[i]);
//        }
//        for (int i =1; i < m-1; i++)
//        {
//            int ind = 2*m*n-i*(m-1);
//            l[i] = max(0.0, l[i] - ro * x[ind+k]);
//        }
//        for (int i = 0; i < m; i++)
//            lOld[i] = l[i];
//        iter++;
    }
    return ;
}

void UzawaGaussSeidelSolver::setRo(double Ro)
{
    ro = Ro;
}

void UzawaGaussSeidelSolver::setUzawaEps(double UzawaEps)
{
    uzawaEps = UzawaEps;
}

void UzawaGaussSeidelSolver::stop()
{
    isStopped = true;
    SimpleSolver::stop();
}
